<template>
  <el-dialog
    :before-close="beforeClose"
    :visible.sync="show"
    :title="title"
    width="30%"
    @close="close"
    @closed="closed"
  >
    <el-form label-width="80px" :model="form">
      <el-form-item label="问题描述" required>
        <el-input v-model="form.content" type="textarea" rows="5" maxlength="200" show-word-limit />
      </el-form-item>
      <el-form-item label="上传截图(选填)">
        <el-upload
          class="avatar-uploader"
          action="/api/upload/index"
          :show-file-list="false"
          :on-success="handleAvatarSuccess"
          :before-upload="beforeAvatarUpload"
          :headers="headers"
        >
          <img v-if="imageUrl" :src="imageUrl" class="avatar">
          <i v-else class="el-icon-plus avatar-uploader-icon" />
        </el-upload>
      </el-form-item>
      <el-form-item label="联系方式(选填)">
        <el-input v-model="form.mobile" />
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="handleClose">取 消</el-button>
      <el-button type="primary" @click="handleSubmit">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import store from '@/store'
import { getAddress } from '@/api/system'
export default {
  name: 'Index',
  props: {
    visible: {
      type: Boolean,
      default: false
    }

  },
  data() {
    return {
      show: false,
      form: {
        content: '',
        mobile: '',
        pic: ''
      },
      imageUrl: '',
      baseUrl: ''
    }
  },
  computed: {
    title() {
      return '问题反馈/建议'
    },
    headers() {
      return { authorization: 'Bearer ' + store.getters.token }
    }
  },
  watch: {
    visible(val) {
      if (val) {
        this.getData()
        this.show = val
        this.form = {
          content: '',
          mobile: '',
          pic: ''
        }
      }
    }
  },
  mounted() {
    this.show = this.visible
  },
  methods: {
    handleChange(value) {
      console.log(value)
    },
    getData() {
      getAddress().then(p => {
        this.baseUrl = p.data
      })
      console.log('获取数据')
    },
    handleClose() {
      console.log('点击关闭')
      this.beforeClose()
    },
    handleSubmit() {
      console.log('点击确定')
      this.$notify({
        title: 'Success',
        dangerouslyUseHTMLString: true,
        message: `
            提交成功!
          `,
        type: 'success'
      })
      this.handleClose()
    },
    handleAvatarSuccess(res, file) {
      // console.log(res)
      const { data } = res
      // console.log(data)
      this.imageUrl = this.baseUrl + data
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg'
      const isLt2M = file.size / 1024 / 1024 < 5

      if (!isJPG) {
        this.$message.error('上传图片只能是 JPG 格式!')
      }
      if (!isLt2M) {
        this.$message.error('上传图片大小不能超过 5MB!')
      }
      return isJPG && isLt2M
    },
    beforeClose() {
      console.log('关闭前的回调')
      this.show = false
      this.$emit('update:visible', false)
      this.$emit('refresh')
    },
    close() {
      console.log('关闭的回调')
    },
    closed() {
      console.log('关闭动画结束后的回调')
    }
  }
}
</script>

<style scoped>
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }

</style>
